package cn.com.ebidding.monitor.autoconfig;

import cn.com.ebidding.monitor.service.MonitorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author qiyang
 * @Date 2021年2月26日
 * 监控自动配置类
 */
@Configuration
@EnableConfigurationProperties(MonitorProperties.class)
@ConditionalOnProperty(
        prefix = "ebs.monitor",
        name = "enabled",
        havingValue = "true"
)
public class MonitorAutoConfiguration {

    private final String DEFAULT_IP = "127.0.0.1";

    private final Integer DEFAULT_PORT = 8000;

    @Autowired
    private MonitorProperties monitorProperties;

    @Bean(name = "ebsMonitor")
    public MonitorService monitorService() {
        System.out.println(" MonitorAutoConfiguration 启用，初始化 MonitorService");

        if (null == monitorProperties.getIp()) {
            monitorProperties.setIp(DEFAULT_IP);
        }

        if (null == monitorProperties.getPort()) {
            monitorProperties.setPort(DEFAULT_PORT);
        }

        return new MonitorService(monitorProperties.getIp(), monitorProperties.getPort());
    }

}
